package com.zhu.day.learn;

import com.google.common.util.concurrent.RateLimiter;

import java.time.LocalDateTime;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 令牌桶算法
 */
public class RateLimiters {
    private static RateLimiter rateLimiter;
    static {
        rateLimiter = RateLimiter.create(2);
    }

    public static void main(String[] args) {
        // 每秒生成2个令牌
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                // 获得令牌
                rateLimiter.acquire();
                System.out.println(LocalDateTime.now());
            }).start();
        }

        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                // 获得令牌
                rateLimiter.acquire();
                System.out.println(LocalDateTime.now());
            }).start();
        }
    }
}
